package de.hsos.bose.skillshare.model.core.table;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import de.hsos.bose.skillshare.model.core.DataAccess;
import de.hsos.bose.skillshare.model.objects.SkillGruppe;

public class GroupTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	private static final String resGroupName = "Group";
	private static final String resGroupDescription = "Description";

	public static final int GROUP_NAME = 0;
	public static final int GROUP_DESCRIPTION = 1;

	protected String[] columnNames = { resGroupName, resGroupDescription };
	protected Vector<SkillGruppe> dataVector;
	
	public GroupTableModel() {
		this.dataVector = new Vector<SkillGruppe>();
	}
	
	public List<SkillGruppe> getDirty(){
		List<SkillGruppe> dirtyList = new ArrayList<SkillGruppe>();
		for (SkillGruppe group : dataVector) {
			if (group.isDirty()){
				dirtyList.add(group);
			}
		}
		return dirtyList;
	}
	
	public List<SkillGruppe> getNew(){
		List<SkillGruppe> newList = new ArrayList<SkillGruppe>();
		for (SkillGruppe group : dataVector) {
			if (group.isNew()){
				newList.add(group);
			}
		}
		return newList;
	}
	
	public void addEmptyRow(){
		SkillGruppe newGroup = new SkillGruppe();
		newGroup.setNew(true);
		dataVector.add(newGroup);
		fireTableRowsInserted(dataVector.size(), dataVector.size());
	}
	
	public void loadGroups() {
		while(dataVector.size() > 0){
			dataVector.remove(0);
			fireTableRowsDeleted(0, 0);
		}
		DataAccess da = new DataAccess();
		List<SkillGruppe> groupList = da.getSkillGroups();

		for (SkillGruppe group : groupList) {
			dataVector.add(group);
			fireTableRowsInserted(dataVector.size(), dataVector.size());
		}
	}
	
	public SkillGruppe getSkillGruppe(int index) {
		return dataVector.get(index);
	}

	@Override
	public String getColumnName(int column) {
		return columnNames[column];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return true;
	}
	
	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
			case GROUP_NAME:
			case GROUP_DESCRIPTION:
				return String.class;
			default:
				return Object.class;
		}
	}
	
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		SkillGruppe record = (SkillGruppe) dataVector.get(rowIndex);
		switch (columnIndex) {
		case GROUP_NAME:
			return record.getSkillGruppenName();
		case GROUP_DESCRIPTION:
			return record.getSkillGruppenBeschreibung();
		default:
			return new Object();
		}
	}
	
	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		SkillGruppe record = (SkillGruppe) dataVector.get(rowIndex);
		switch (columnIndex) {
		case GROUP_NAME:
			record.setSkillGruppenName((String) aValue);
			break;
		case GROUP_DESCRIPTION:
			record.setSkillGruppenBeschreibung((String) aValue);
			break;
		default:
			System.out.println("invalid index");
		}
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return dataVector.size();
	}

}
